home *** CD-ROM | disk | FTP | other *** search
- #pragma once
-
- #ifndef __REFERENCETEMPLATES__
- #define __REFERENCETEMPLATES__
-
- //
- // For REQUIREVALIDPOINTER
- //
- #include "Debug.h"
-
- //
- // Still needed?
- //
- #include "Exceptions.h"
-
- //
- // For nil
- //
- #include <Types.h>
-
- //
- // A modifiable reference to an object of type T
- //
- template <class T> class AnUpdate
- {
- private:
- T* fRecord;
-
- public:
- AnUpdate() : fRecord(nil) {}
- AnUpdate(T* record) : fRecord(record) { this->AddReference(); }
- AnUpdate(const AnUpdate<T>& record) : fRecord(record.fRecord) { this->AddReference(); }
- ~AnUpdate() { this->RemoveReference(); }
-
- Boolean Exists() const { return fRecord != nil; }
-
- void AddReference() const;
- void RemoveReference() const;
-
- operator T*() { return fRecord; }
- operator const T*() const { return fRecord; }
-
- AnUpdate<T>& operator=(const AnUpdate<T>& rhs)
- {
- if(this->fRecord != rhs.fRecord)
- {
- this->RemoveReference();
- this->fRecord = rhs.fRecord;
- this->AddReference();
- }
-
- return *this;
- }
-
- AnUpdate<T>& operator=(T* rhs)
- {
- if(this->fRecord != rhs)
- {
- this->RemoveReference();
- this->fRecord = rhs;
- this->AddReference();
- }
-
- return *this;
- }
-
- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- // Operator->:
- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
-
- T* operator->() { REQUIREVALIDPOINTER(fRecord); return fRecord; }
- const T* operator->() const { REQUIREVALIDPOINTER(fRecord); return fRecord; }
-
- };
-
- //
- // A non-modifiable reference to a typed object
- //
- template <class T> class AConst
- {
- private:
- const T* fRecord;
-
- public:
- AConst() : fRecord(nil) {}
- AConst(const T* record) : fRecord(record) { this->AddReference(); }
- AConst(const AConst<T>& record) : fRecord(record.fRecord) { this->AddReference(); }
- AConst(const AnUpdate<T>& record) : fRecord(record.operator const T*()) { this->AddReference(); }
- ~AConst() { this->RemoveReference(); }
-
- Boolean Exists() const { return fRecord != nil; }
-
- void AddReference() const;
- void RemoveReference() const;
-
- //
- // C++ Snafu:
- //
- // Probably don't want to include this for the non-modifiable reference template
- //
- // With this, AnUpdate<T> x(AConst<T>(y)) is a warning; without this, it is
- // an error (which is what we would prefer).
- //
- // However, without this it is an error to return an AConst<x> in place
- // of an AConst<y> if x is derived from y. We don't need this error.
- //
- operator const T*() const { return fRecord; }
-
- AConst<T>& operator=(const AConst<T>& rhs)
- {
- if(this->fRecord != rhs.fRecord)
- {
- this->RemoveReference();
- this->fRecord = rhs.fRecord;
- this->AddReference();
- }
-
- return *this;
- }
-
- AConst<T>& operator=(const T* rhs)
- {
- if(this->fRecord != rhs)
- {
- this->RemoveReference();
- this->fRecord = rhs;
- this->AddReference();
- }
-
- return *this;
- }
-
- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- // Operator->:
- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
-
- const T* operator->() const { REQUIREVALIDPOINTER(fRecord); return fRecord; }
-
- };
-
- //--------------------------------------------------------------------------------
- // AConst<T>::AddReference
- //--------------------------------------------------------------------------------
- template <class T>
- void AConst<T>::AddReference() const
- {
- if(fRecord != nil)
- fRecord->AddReference();
- } // AConst<T>::AddReference
-
- //--------------------------------------------------------------------------------
- // AConst<T>::RemoveReference
- //--------------------------------------------------------------------------------
- template <class T>
- void AConst<T>::RemoveReference() const
- {
- if(fRecord != nil)
- fRecord->RemoveReference();
- } // AConst<T>::RemoveReference
-
- //--------------------------------------------------------------------------------
- // AnUpdate<T>::AddReference
- //--------------------------------------------------------------------------------
- template <class T>
- void AnUpdate<T>::AddReference() const
- {
- if(fRecord != nil)
- fRecord->AddReference();
- } // AnUpdate<T>::AddReference
-
- //--------------------------------------------------------------------------------
- // AnUpdate<T>::RemoveReference
- //--------------------------------------------------------------------------------
- template <class T>
- void AnUpdate<T>::RemoveReference() const
- {
- if(fRecord != nil)
- fRecord->RemoveReference();
- } // AnUpdate<T>::RemoveReference
-
- #endif
-
-